/*
 *  Copyright 2016 Google Inc. All Rights Reserved.
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.google.blockly.ohos.demo.unit;

import com.google.blockly.model.Field;
import com.google.blockly.model.FieldAngle;
import org.junit.Assert;
import org.junit.Test;

/**
 * Tests for {@link FieldAngle}.
 */
public class FieldAngleTest {
    @Test
    public void testConstructor() {
        FieldAngle field = new FieldAngle("name", 0);

        Assert.assertEquals(Field.TYPE_ANGLE, field.getType());
        Assert.assertEquals("name", field.getName());
        Assert.assertEquals(0, (int)field.getAngle());
    }

    @Test
    public void testWrapAround() {
        FieldAngle field = new FieldAngle("name", 360);
        Assert.assertEquals(0, (int)field.getAngle());

        field.setAngle(720f);
        Assert.assertEquals(0, (int)field.getAngle());

        field.setAngle(-180f);
        Assert.assertEquals(180, (int)field.getAngle());

        field.setAngle(10000f);
        Assert.assertEquals(280, (int)field.getAngle());

        field.setAngle(-10000f);
        Assert.assertEquals(80, (int)field.getAngle());

        field.setAngle(27f);
        Assert.assertEquals(27, (int)field.getAngle());

        field.setAngle(-10001f);
        Assert.assertEquals(79, (int)field.getAngle());
    }

    @Test
    public void testParseValue() {
        FieldAngle field = new FieldAngle("name", 0);

        // xml parsing
        Assert.assertTrue(field.setFromString("-180"));
        Assert.assertEquals(180, (int)field.getAngle());
        Assert.assertTrue(field.setFromString("27"));
        Assert.assertEquals(27, (int)field.getAngle());
        Assert.assertFalse(field.setFromString("this is not a number"));
    }

    @Test
    public void testClone() {
        FieldAngle field = new FieldAngle("name", 5);
        FieldAngle clone = field.clone();
        Assert.assertNotSame(field.clone(), field);
        Assert.assertEquals(field.getName(), clone.getName());
        Assert.assertEquals((int)field.getAngle(), (int)clone.getAngle());
    }
}
